package jmine.tec.subscription.impl;

import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import jmine.tec.subscription.SubscriptionSettings;

/**
 * Implementacao padrao de {@link SubscriptionSettings} que usa um mapa para guardar os parametros
 * 
 * @author takeshi
 */
public class PlainSubscriptionSettings implements SubscriptionSettings, Serializable {

    private static final long serialVersionUID = -4725090186014135903L;

    private final Map<String, Object> parameters;

    /**
     * C'tor
     * 
     * @param parameters {@link Map} de {@link String} para {@link Object}
     */
    public PlainSubscriptionSettings(Map<String, Object> parameters) {
        super();
        this.parameters = Collections.unmodifiableMap(new HashMap<String, Object>(parameters));
    }

    /**
     * {@inheritDoc}
     */
    public Map<String, Object> getParameters() {
        return this.parameters;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((this.parameters == null) ? 0 : this.parameters.hashCode());
        return result;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (this.getClass() != obj.getClass()) {
            return false;
        }
        PlainSubscriptionSettings other = (PlainSubscriptionSettings) obj;
        if (this.parameters == null) {
            if (other.parameters != null) {
                return false;
            }
        } else if (!this.parameters.equals(other.parameters)) {
            return false;
        }
        return true;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return "settings: " + this.parameters;
    }

    /**
     * {@inheritDoc}
     */
    public <T> T getParameter(String parameterName, Class<T> parameterType) {
        return parameterType.cast(this.parameters.get(parameterName));
    }

}
